package group32.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Node;


public class FriendBrowser extends Composite {
	
	
	VerticalPanel graph=new VerticalPanel();
	
	/**
	 * This is the graph visualization as an object
	 */
	private JavaScriptObject theGraph;
	
	int curUser;
	
	private FriendBrowseServiceAsync pService = GWT
			.create(FriendBrowseService.class);
	
	
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";
	
	DialogBox page= new DialogBox();
	
	public FriendBrowser(final int userID,String utype){
		curUser=userID;
		DOM.setInnerHTML(graph.getElement(), "");
		History.newItem("BrowseFriends");
		DOM.setInnerHTML(graph.getElement(), "<div id=\"infovis\"></div> ");
		visualizeInitialPerson(userID,utype);
		initWidget(graph);
	}
	/**
	 * Request the ID of the first person in the data, then visualize him/her
	 * 
	 */
	public void visualizeInitialPerson(int person,String utype) {
						// Request the person and his/her neighbors
				pService.getPersonAndNeighbours(person,utype, new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Unable to execute XQuery");
						
					}

					@Override
					public void onSuccess(String result) {
						try {
							//System.out.println(result);
							JSONObject x = XMLandJSON.getJsonForUser(XMLandJSON.parseXML(result));
							if (x != null) {
								System.out.println(result);
								
								theGraph = FriendBrowse.createGraph(x.toString(), FriendBrowser.this,curUser);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
		
	}
	
	
	/**
	 * Incrementally redraw graph for the selected person.
	 * Also update the left and right-hand panes
	 * 
	 * @param result
	 */
	public void drawNodeAndNeighbors(final int person,String type) {
		//System.out.println(person);
		pService.getPersonAndNeighbours(person,type, new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(SERVER_ERROR);
				
			}

			@Override
			public void onSuccess(String result) {
				Node n = XMLandJSON.parseXML(result);
				
				if (n != null) {
					if(FriendBrowse.level!=-1)
					FriendBrowse.addToGraph(theGraph,XMLandJSON.getJsonForUser(n).toString(),curUser,person);
				}
			}
			
		});
	}
	
	public void getProfile(int uid,String type,String logon){
		HomePage.showProfile(uid,type,logon);
	}
}
